package com.ar.studyapp;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.internal.widget.IcsAdapterView;
import com.actionbarsherlock.internal.widget.IcsAdapterView.OnItemSelectedListener;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.ar.adapter.IndexAdapter;
import com.ar.bean.Exam;
import com.ar.customview.AbExamTitle;
import com.ar.datahandler.StringJSONHandler;
import com.ar.network.NetworkUtility;
import com.ar.network.OnResponseListener;
import com.ar.network.RequestBuilder;
import com.ar.network.SOAPConnector;
import com.loopj.android.image.SmartImageView;

/**
 * The Class ExamActivity.
 */
public class ExamActivity extends BaseActivity implements
		OnCheckedChangeListener, OnGestureListener, OnResponseListener {

	/** The tv question1st. */
	private TextView tvQuestion1st;

	/** The iv illutration1st. */
	private SmartImageView ivIllutration1st;

	/** The rdg answers1st. */
	private RadioGroup rdgAnswers1st;

	/** The tv question2nd. */
	private TextView tvQuestion2nd;

	/** The iv illutration2nd. */
	private SmartImageView ivIllutration2nd;

	/** The rdg answers2nd. */
	private RadioGroup rdgAnswers2nd;

	/** The view switcher. */
	private ViewSwitcher viewSwitcher;

	/** The exam title. */
	private AbExamTitle examTitle;

	/** The json worker. */
	private StringJSONHandler jsonWorker;

	/** The data. */
	private Exam data;

	/** The index adapter. */
	private IndexAdapter indexAdapter;

	/** The m current quiz number. */
	private int mCurrentQuizNumber;

	/** The num_quizs. */
	private int num_quizs;

	/** The submited. */
	private boolean submited;

	/** The user answers. */
	private int[] userAnswers;

	/** The correct_answers. */
	private int[] correct_answers;

	/** The ans i ds. */
	private ArrayList<Integer> ansIDs;

	/** The is visible page. */
	private boolean isVisiblePage;

	/** The gesture. */
	private GestureDetector gesture = null;

	/** The time count down. */
	private CountDownTimer timeCountDown;

	/** The time remain. */
	private String timeRemain;

	/** The checked. */
	private boolean checked;

	/** The progress dialog on check. */
	private ProgressDialog progressDialogOnCheck;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.studyapp.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.studyapp.BaseActivity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		if (checked) {

				boolean[] ansFlags = getIntent().getBooleanArrayExtra(
						"ansFlags");
				int position = getIntent().getExtras().getInt("position");
				correct_answers = this.getIntent().getIntArrayExtra(
						"correct answers");
				mCurrentQuizNumber = position;
				indexAdapter.setCheckedState(true);
				indexAdapter.setAnswerFlags(ansFlags);
		}
		invalidateQuiz();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ar.studyapp.BaseActivity#onPrepareOptionsMenu(com.actionbarsherlock
	 * .view.Menu)
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem(R.id.item_check).setVisible(!checked);
		menu.findItem(R.id.item_view_result).setVisible(checked);
		menu.findItem(R.id.item_time_count_down).setEnabled(!submited);
		menu.findItem(R.id.item_time_count_down).setTitle(timeRemain);
		return super.onPrepareOptionsMenu(menu);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.actionbarsherlock.app.SherlockActivity#onOptionsItemSelected(android
	 * .view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item_check:
			if (!submited)
				showCheckMarkDialog();
			else {
				if (NetworkUtility.checkNetworkState(this)) {
					progressDialogOnCheck = ProgressDialog.show(this,
							"Chấm điểm", "Đang tải lại kết quả …");
					startCheckMarkRequest();
				} else
					NetworkUtility.showNoNetworkDialog(this);
			}
			break;
		case R.id.item_view_result:
			viewResult();

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.studyapp.BaseActivity#displayActivity()
	 */
	@Override
	protected void displayActivity() {
		setContentView(R.layout.activity_exam);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.studyapp.BaseActivity#initViews()
	 */
	@Override
	protected void initViews() {
		viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
		tvQuestion1st = (TextView) findViewById(R.id.tvQuestion1st);
		tvQuestion2nd = (TextView) findViewById(R.id.tvQuestion2nd);
		ivIllutration1st = (SmartImageView) findViewById(R.id.ivIllustration1st);
		ivIllutration2nd = (SmartImageView) findViewById(R.id.ivIllustration2nd);
		rdgAnswers1st = (RadioGroup) findViewById(R.id.rdgAnswers1st);
		rdgAnswers2nd = (RadioGroup) findViewById(R.id.rdgAnswers2nd);
		rdgAnswers1st.setOnCheckedChangeListener(this);
		rdgAnswers2nd.setOnCheckedChangeListener(this);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.studyapp.BaseActivity#initVariables()
	 */
	@Override
	protected void initVariables() {
		checked = false;
		jsonWorker = new StringJSONHandler();
		ansIDs = new ArrayList<Integer>();
		mCurrentQuizNumber = 1;
		num_quizs = 0;
		submited = false;
		isVisiblePage = true;
		gesture = new GestureDetector(getApplicationContext(), this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.studyapp.BaseActivity#initActions()
	 */
	@Override
	protected void initActions() {
		String json = this.getIntent().getExtras().getString("data");
		Log.i("json", json);
		startExam(json);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.studyapp.BaseActivity#setUpActionBar()
	 */
	@Override
	protected void setUpActionBar() {
		super.setUpActionBar();
		ActionBar bar = getSupportActionBar();
		setHideActionBarItems(false);
		bar.setDisplayShowTitleEnabled(false);
		bar.setDisplayShowHomeEnabled(false);
		bar.setDisplayShowCustomEnabled(true);
		// ArrayList<String> data = new ArrayList<String>();
		// for (int i = 0; i<15; i++) data.add(new String("Câu " + i));
		indexAdapter = new IndexAdapter(this, num_quizs, userAnswers);
		examTitle = new AbExamTitle(this, indexAdapter, getIntent()
				.getStringExtra("title"));
		examTitle
				.setOnSpinnerIndexSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(IcsAdapterView<?> parent,
							View view, int position, long id) {
						// if (position + 1 != mCurrentQuizNumber){
						mCurrentQuizNumber = position + 1;
						if (isVisiblePage) {
							showQuiz(mCurrentQuizNumber, tvQuestion1st,
									ivIllutration1st, rdgAnswers1st);
						} else {
							showQuiz(mCurrentQuizNumber, tvQuestion2nd,
									ivIllutration2nd, rdgAnswers2nd);
						}

						// }
					}

					@Override
					public void onNothingSelected(IcsAdapterView<?> parent) {

					}
				});
		bar.setCustomView(examTitle);
		invalidateOptionsMenu();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.RadioGroup.OnCheckedChangeListener#onCheckedChanged(android
	 * .widget.RadioGroup, int)
	 */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (checkedId != -1)
			userAnswers[mCurrentQuizNumber - 1] = checkedId;

	}

	/**
	 * Show quiz.
	 * 
	 * @param CurrentQuizNumber
	 *            the current quiz number
	 * @param tvQuestion
	 *            the tv question
	 * @param imageView
	 *            the image view
	 * @param rdgAnswers
	 *            the rdg answers
	 */
	private void showQuiz(int CurrentQuizNumber, TextView tvQuestion,
			SmartImageView imageView, RadioGroup rdgAnswers) {

		// String qHead = String.valueOf(CurrentQuizNumber);
		if (CurrentQuizNumber <= num_quizs && CurrentQuizNumber >= 1) {
			char ansPrefix = 'A';
			String question = data.getContent().get(CurrentQuizNumber - 1)
					.getQuestion();
			tvQuestion.setText(Html.fromHtml(question));
			String imgUrl = data.getContent().get(CurrentQuizNumber - 1)
					.getImageUrl();
			if (imgUrl.equals("\"\"")) {
				imageView.setVisibility(View.GONE);
			} else {
				imageView.setVisibility(View.VISIBLE);
				imageView.setImageUrl(data.getContent()
						.get(CurrentQuizNumber - 1).getImageUrl());
			}
			rdgAnswers.removeAllViews();

			for (int i = 0; i < data.getContent().get(CurrentQuizNumber - 1)
					.getAnswers().length; i++) {
				if (i > 0)
					ansPrefix++;
				RadioButton radioButton = new RadioButton(this);
				radioButton.setText(new String(ansPrefix
						+ ". "
						+ data.getContent().get(CurrentQuizNumber - 1)
								.getAnswers()[i]));
				radioButton.setId(i);
				radioButton.setButtonDrawable(R.drawable.btn_radio_holo_light);
				rdgAnswers.addView(radioButton);
				rdgAnswers.clearCheck();
				rdgAnswers.check(userAnswers[CurrentQuizNumber - 1]);
				if (submited)
					radioButton.setEnabled(false);
			}
			if (correct_answers != null) {
				Log.d("correct_answers", "correct_answers!= null");
				int correctID = correct_answers[CurrentQuizNumber - 1];
				String abc = "<b><u><font color='#EE0000'>"
						+ StringJSONHandler.toWord(correctID)
						+ "</font></u></b>"
						+ ". "
						+ data.getContent().get(CurrentQuizNumber - 1)
								.getAnswers()[correctID];
				((RadioButton) rdgAnswers
						.getChildAt(correct_answers[CurrentQuizNumber - 1]))
						.setText(Html.fromHtml(abc));
			}

		}

	}

	/**
	 * Navigate quiz.
	 * 
	 * @param isNext
	 *            the is next
	 */
	private void navigateQuiz(boolean isNext) {
		if ((isNext && mCurrentQuizNumber == num_quizs)
				|| (!isNext && mCurrentQuizNumber == 1))
			return;
		if (isNext) {
			if (mCurrentQuizNumber == num_quizs)
				mCurrentQuizNumber = 1;
			else
				mCurrentQuizNumber++;
		} else {
			if (mCurrentQuizNumber == 1)
				mCurrentQuizNumber = num_quizs;
			else
				mCurrentQuizNumber--;
		}

		if (isVisiblePage) {
			showQuiz(mCurrentQuizNumber, tvQuestion2nd, ivIllutration2nd,
					rdgAnswers2nd);
			isVisiblePage = false;
		} else {
			showQuiz(mCurrentQuizNumber, tvQuestion1st, ivIllutration1st,
					rdgAnswers1st);
			isVisiblePage = true;
		}

		if (isNext) {
			viewSwitcher.setInAnimation(this, R.anim.in_animation1);
			viewSwitcher.setOutAnimation(this, R.anim.out_animation1);
			viewSwitcher.showNext();
		} else {
			viewSwitcher.setInAnimation(this, R.anim.in_animation);
			viewSwitcher.setOutAnimation(this, R.anim.out_animation);
			viewSwitcher.showPrevious();
		}
		examTitle.select(mCurrentQuizNumber - 1);
	}

	/**
	 * On click next.
	 * 
	 * @param view
	 *            the view
	 */
	public void onClickNext(View view) {
		navigateQuiz(true);
	}

	/**
	 * On click previous.
	 * 
	 * @param view
	 *            the view
	 */
	public void onClickPrevious(View view) {
		navigateQuiz(false);
	}

	/**
	 * Invalidate quiz.
	 */
	private void invalidateQuiz() {
		if (isVisiblePage) {
			showQuiz(mCurrentQuizNumber, tvQuestion1st, ivIllutration1st,
					rdgAnswers1st);
		} else {
			showQuiz(mCurrentQuizNumber, tvQuestion2nd, ivIllutration2nd,
					rdgAnswers2nd);
		}
		examTitle.select(mCurrentQuizNumber - 1);
	}
	
	private void startExam(String jsonData){
		data = jsonWorker.fillDataFromResponse(jsonData);
		num_quizs = data.getContent().size();
		userAnswers = new int[num_quizs];
		for (int i = 0; i < num_quizs; i++)
			userAnswers[i] = -1;
		long time = Long.valueOf(new String("" + data.getTime())) * 60000;
		timeCountDown = new CountDownTimer(time, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				millisUntilFinished = millisUntilFinished / 1000;
				timeRemain = new String(millisUntilFinished / 60 + " : "
						+ millisUntilFinished % 60);
				supportInvalidateOptionsMenu();
			}

			@Override
			public void onFinish() {
				stopExaming();
				timeRemain = new String("0:0");
				showTimeOutDialog();

			}
		};
		timeCountDown.start();
	}

	/**
	 * Stop examing.
	 */
	private void stopExaming() {
		timeCountDown.cancel();
		submited = true;
		supportInvalidateOptionsMenu();
		invalidateQuiz();
	}

	/**
	 * Start check mark request.
	 */
	private void startCheckMarkRequest() {
		// on request
		// JSONObject jsonRequest = new JSONObject();
		// try {
		// jsonRequest.put(StringJSONHandler.HANDLE, StringJSONHandler.ONCHECK);
		// JSONArray jsonResult = new JSONArray();
		if (ansIDs.size() != userAnswers.length)
			for (int i = 0; i < userAnswers.length; i++) {
				// JSONObject jsonObject = new JSONObject();
				int id = data.getContent().get(i).getId();
				// jsonObject.put(StringJSONHandler.ID, id);
				ansIDs.add(id);
				// jsonObject.put(StringJSONHandler.CHOOSE, userAnswers[i]);
			}
		// jsonRequest.put(StringJSONHandler.RESULTS, jsonResult);
		// } catch (JSONException e1) {
		// e1.printStackTrace();
		// }

		// response
		// JSONObject jsonResponse = new JSONObject();
		// JSONArray jsonArray = new JSONArray();
		// JSONObject jsonObject = new JSONObject();
		// JSONObject jsonObject1 = new JSONObject();
		// JSONObject jsonObject2 = new JSONObject();
		// try {
		// jsonObject.put(StringJSONHandler.ID, 1);
		// jsonObject.put(StringJSONHandler.CHECK, true);
		// jsonObject.put(StringJSONHandler.TRUE_ANSWERS, new JSONArray("[1]"));
		// jsonArray.put(jsonObject);
		//
		// jsonObject1.put(StringJSONHandler.ID, 2);
		// jsonObject1.put(StringJSONHandler.CHECK, false);
		// jsonObject1.put(StringJSONHandler.TRUE_ANSWERS, new
		// JSONArray("[2]"));
		// jsonArray.put(jsonObject1);
		//
		// jsonObject2.put(StringJSONHandler.ID, 3);
		// jsonObject2.put(StringJSONHandler.CHECK, true);
		// jsonObject2.put(StringJSONHandler.TRUE_ANSWERS, new
		// JSONArray("[2]"));
		// jsonArray.put(jsonObject2);
		//
		// jsonResponse.put(StringJSONHandler.MARK, 10);
		// jsonResponse.put(StringJSONHandler.RESULTS, jsonArray);
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }

		SOAPConnector connector = new SOAPConnector(
				RequestBuilder.BuildOnCheckRequest(ansIDs, userAnswers));
		connector.setOnResponseListener(this);
		connector.startRequest();
	}

	/**
	 * View result.
	 */
	private void viewResult() {
		Intent intent = new Intent(getApplicationContext(),
				ResultActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#dispatchTouchEvent(android.view.MotionEvent)
	 */
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		super.dispatchTouchEvent(ev);
		return gesture.onTouchEvent(ev);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gesture.onTouchEvent(event);
	}

	/**
	 * On down.
	 * 
	 * @param e
	 *            the e
	 * @return true, if successful
	 */
	@Override
	public boolean onDown(MotionEvent e) {

		return false;
	}

	/** The swipe min velocity. */
	int SWIPE_MIN_VELOCITY = 50;

	/** The swipe min distance. */
	int SWIPE_MIN_DISTANCE = 50;

	/**
	 * On fling.
	 * 
	 * @param e1
	 *            the e1
	 * @param e2
	 *            the e2
	 * @param velocityX
	 *            the velocity x
	 * @param velocityY
	 *            the velocity y
	 * @return true, if successful
	 */
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		Log.d("Switch", "onFling");

		// Get Position
		float ev1X = e1.getX();
		float ev2X = e2.getX();

		// Get distance of X (e1) to X (e2)
		final float xdistance = Math.abs(ev1X - ev2X);
		// Get veclocity of cusor
		// Vận tốc = số điểm ảnh (px) / giây
		final float xvelocity = Math.abs(velocityX);

		// Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm
		// kéo cuối > 100
		if ((xvelocity > SWIPE_MIN_VELOCITY)
				&& (xdistance > SWIPE_MIN_DISTANCE)) {
			if (ev1X > ev2X)// Switch Left
			{
				Log.d("Switch", "Right");
				navigateQuiz(true);
			} else// Switch Right
			{
				Log.d("Switch", "Left");
				navigateQuiz(false);
			}
		}
		return false;
	}

	/**
	 * On long press.
	 * 
	 * @param e
	 *            the e
	 */
	@Override
	public void onLongPress(MotionEvent e) {

	}

	/**
	 * On scroll.
	 * 
	 * @param e1
	 *            the e1
	 * @param e2
	 *            the e2
	 * @param distanceX
	 *            the distance x
	 * @param distanceY
	 *            the distance y
	 * @return true, if successful
	 */
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {

		return false;
	}

	/**
	 * On show press.
	 * 
	 * @param e
	 *            the e
	 */
	@Override
	public void onShowPress(MotionEvent e) {

	}

	/**
	 * On single tap up.
	 * 
	 * @param e
	 *            the e
	 * @return true, if successful
	 */
	@Override
	public boolean onSingleTapUp(MotionEvent e) {

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onNewIntent(android.content.Intent)
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		showConfirmDialog();
	}

	/**
	 * Show confirm dialog.
	 */
	private void showConfirmDialog() {
		AlertDialog.Builder restartAlertBuilder = new AlertDialog.Builder(this);
		restartAlertBuilder.setTitle("Warning");
		restartAlertBuilder
				.setMessage("This will back to main screen, are you sure ?");
		restartAlertBuilder.setCancelable(false);
		restartAlertBuilder.setPositiveButton("Ok", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i = new Intent(getBaseContext(),
						CategoriesActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
			}
		});
		restartAlertBuilder.setNegativeButton("Nope", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});

		AlertDialog restartAlertDialog = restartAlertBuilder.create();
		restartAlertDialog.show();

	}

	/**
	 * Show check mark dialog.
	 */
	private void showCheckMarkDialog() {
		int unAnsweredCount = 0;
		for (int ans : userAnswers) {
			if (ans == -1)
				unAnsweredCount++;
		}
		String msg;
		if (unAnsweredCount > 0)
			msg = new String(
					"You still have "
							+ unAnsweredCount
							+ " uncheck question, are you sure you want to finish the exam and check mark ?");
		else
			msg = "This will stop the exam and process to check mark, are you sure ?";
		AlertDialog.Builder restartAlertBuilder = new AlertDialog.Builder(this);
		restartAlertBuilder.setTitle("Warning");
		restartAlertBuilder.setMessage(msg);
		restartAlertBuilder.setCancelable(false);
		restartAlertBuilder.setPositiveButton("Yes", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				stopExaming();
				if (NetworkUtility.checkNetworkState(getApplicationContext())) {
					progressDialogOnCheck = ProgressDialog.show(
							getActivityInstance(), "Checking mark",
							"Loading result from server, please wait …");
					startCheckMarkRequest();
				} else
					NetworkUtility.showNoNetworkDialog(getActivityInstance());

			}
		});
		restartAlertBuilder.setNegativeButton("No", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});

		AlertDialog restartAlertDialog = restartAlertBuilder.create();
		restartAlertDialog.show();

	}

	/**
	 * Show time out dialog.
	 */
	private void showTimeOutDialog() {
		AlertDialog.Builder restartAlertBuilder = new AlertDialog.Builder(this);
		restartAlertBuilder.setTitle("The time's up");
		restartAlertBuilder
				.setMessage("You're out of time, press 'Ok' to check your mark");
		restartAlertBuilder.setCancelable(false);
		restartAlertBuilder.setPositiveButton("Ok", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (NetworkUtility.checkNetworkState(getActivityInstance())) {
					progressDialogOnCheck = ProgressDialog.show(
							getActivityInstance(), "Cheking mark",
							"Loading result form server, please wait …");
					startCheckMarkRequest();
				} else
					NetworkUtility.showNoNetworkDialog(getActivityInstance());
			}
		});

		AlertDialog restartAlertDialog = restartAlertBuilder.create();
		restartAlertDialog.show();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.network.OnResponseListener#onResponse(java.lang.String)
	 */
	@Override
	public void onResponse(String jsonResponse) {
		progressDialogOnCheck.dismiss();
		if (jsonWorker.checkError(jsonResponse) == false) {
			checked = true;
			Intent intent = new Intent(this, ResultActivity.class);
			intent.putExtra("answer IDs", ansIDs);
			intent.putExtra("user answers", userAnswers);
			intent.putExtra("result", jsonResponse.toString());
			startActivity(intent);
			supportInvalidateOptionsMenu();
		} else {
			Toast.makeText(getApplicationContext(),
					jsonWorker.getErrorMessage(jsonResponse), Toast.LENGTH_LONG)
					.show();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.network.OnResponseListener#onRequestTimeOut()
	 */
	@Override
	public void onRequestTimeOut() {
		progressDialogOnCheck.dismiss();
		NetworkUtility.showTimeOutDialog(this);
	}

}
